
package com.netease.xone.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.OnPinnedHeaderListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.X1LoadingAdapterViewBase.OnLoadingListener;
import com.netease.xone.R;
import com.netease.xone.widget.listview.ExpandableAdapterWrapper;
import com.netease.xone.widget.listview.LoadingExpandableListView;
import com.netease.xone.widget.listview.LoadingListView;
import com.netease.xone.widget.listview.MultiAdapterManager;
import com.netease.xone.widget.listview.MultiExpandableAdapterManager;
import com.netease.xone.widget.tabview.AbsTabView.OnTabSelectListener;
import com.netease.xone.widget.tabview.ViewInformationCommentTab;

public class test_MultiAdapterList extends ActivityX1Base {

    public static void startActivity(Context context) {
        Intent i = new Intent(context, test_MultiAdapterList.class);
        context.startActivity(i);
    }

    private LoadingListView mListView;
    private TestAdapter mAdapter0, mAdapter1, mAdapter2;
    private MultiAdapterManager mMultiAdapterMgr;

    private ViewInformationCommentTab mHeaderTab;
    private ViewInformationCommentTab mFloatTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity_information_detail);
        mListView = (LoadingListView)findViewById(R.id.test_information_list);
        mListView.setMode(Mode.DISABLED);

        // add header
        View header = View.inflate(this, R.layout.test_view_information_detail, null);
        mListView.getRefreshableView().addHeaderView(header);

        // add tab
        mFloatTab = (ViewInformationCommentTab)findViewById(R.id.float_comment_tab);
        mHeaderTab = new ViewInformationCommentTab(this);

        // 添加固定的Tab
        mListView.setPinnedHeaderIndex(1);// 设置固定Header的位置
        mListView.setOnPinnedHeaderListener(new OnPinnedHeaderListener() {

            @Override
            public void onPinnedHeaderShow() {
                mFloatTab.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPinnedHeaderHide() {
                mFloatTab.setVisibility(View.GONE);
            }
        });

        mFloatTab.setOnTabSelectListener(mTabSelectListener);
        mHeaderTab.setOnTabSelectListener(mTabSelectListener);
        mListView.getRefreshableView().addHeaderView(mHeaderTab);

        // set loading callback
        mListView.setOnLoadingListener(new OnLoadingListener() {

            @Override
            public void onRefreshing() {

            }

            @Override
            public void onLoading() {
                if (mMultiAdapterMgr.getCurrentAdapterIndex() == 1) {
                    new LoadingTask1().execute();
                } else if (mMultiAdapterMgr.getCurrentAdapterIndex() == 2) {
                    new LoadingTask2().execute();
                }
            }

            @Override
            public void onLoadingMore() {
                if (mMultiAdapterMgr.getCurrentAdapterIndex() == 1) {
                    new LoadingMoreTask1().execute();
                }
            }
        });
        // init adapter
        mAdapter0 = new TestAdapter(this, 0);
        mAdapter1 = new TestAdapter(this, 1);
        mAdapter2 = new TestAdapter(this, 2);
        // manager multi adapter
        mMultiAdapterMgr = new MultiAdapterManager(mListView);

        mMultiAdapterMgr.addAdapter(mAdapter0);
        mMultiAdapterMgr.addAdapter(mAdapter1);
        mMultiAdapterMgr.addAdapter(mAdapter2);

        mMultiAdapterMgr.setCurrentAdapter(0);

        // mListView.disableLoadingMore();
        mListView.disablePullToRefresh();
        mListView.onLoadingComplete(false);
    }

    private class LoadingTask1 extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mAdapter1.mCount = 40;
            mAdapter1.notifyDataSetChanged();
            mListView.onLoadingComplete(true);
        }

    }

    private boolean firstload = true;

    private class LoadingTask2 extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (firstload) {
                mListView.onLoadingError();
                firstload = false;
            } else {
                mAdapter2.mCount = 25;
                mAdapter1.notifyDataSetChanged();
                mListView.onLoadingComplete(false);
            }

        }

    }

    private class LoadingMoreTask1 extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mAdapter1.mCount += 20;
            mAdapter1.notifyDataSetChanged();
            if (mAdapter1.mCount < 100) {
                mListView.onLoadingComplete(true);
            } else {
                mListView.onLoadingComplete(false);
            }
        }

    }

    private OnTabSelectListener mTabSelectListener = new OnTabSelectListener() {

        @Override
        public void onTabSelected(View tabView, int index) {
            mMultiAdapterMgr.setCurrentAdapter(index);
            if (tabView == mFloatTab) {
                mHeaderTab.setCurrentTab(index);
            } else if (tabView == mHeaderTab) {
                mFloatTab.setCurrentTab(index);
            }
            if (mListView.getLoadingState() == LoadingListView.STATE_IDLE) {
                mListView.load();
            }
        }

        @Override
        public void onTabReSelected(View tabView, int index) {
            Toast.makeText(test_MultiAdapterList.this, "onTabReselected:" + index,
                    Toast.LENGTH_SHORT).show();
        }

    };

    class TestAdapter extends BaseAdapter {

        private Context mContext;
        int mCount = 0;
        int mColor;

        public TestAdapter(Context context, int color) {
            mContext = context;
            mColor = color;
            if (color == 0) {
                mCount = 25;
            }
        }

        @Override
        public int getCount() {
            return mCount;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new TextView(mContext);
            }
            ((TextView)convertView).setText("position:" + getItem(position));
            if (mColor == 0) {
                ((TextView)convertView).setGravity(Gravity.LEFT);
                convertView.setBackgroundColor(Color.rgb(position * 8, 0, 0));
            } else if (mColor == 1) {
                ((TextView)convertView).setGravity(Gravity.CENTER);
                convertView.setBackgroundColor(Color.rgb(0, position * 8, 0));
            } else {
                ((TextView)convertView).setGravity(Gravity.RIGHT);
                convertView.setBackgroundColor(Color.rgb(0, 0, position * 8));
            }
            return convertView;
        }

    }
}
